package com.bindapp.model;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.os.Parcelable;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bindapp.App;
import com.bindapp.util.RefreshUtil;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by nbzl on 2017/8/11.
 */
public abstract class BaseModel<T extends ViewDataBinding> {

    protected MaterialDialog progressDialog;
    protected final Context context;
    protected App app;
    protected RefreshUtil refreshUtil;

    public BaseModel(Context context){
        this.context=context;
        app=(App)((Activity)context).getApplication();

    }
    protected void showPassDialog(String message) {
        progressDialog = new MaterialDialog.Builder(context)
                .progress(true, 0)
                .content(message)
                .show();
    }

    protected void dissPassDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    public void turnToVc(Class<?> zclass, Map<String,Object> value){
        Intent intent=new Intent(context,zclass);
        for(Map.Entry<String, Object> entry:value.entrySet()){
            if(entry.getValue() instanceof String){
                intent.putExtra(entry.getKey(),entry.getValue().toString());
            }else if(entry.getValue() instanceof Integer){
                intent.putExtra(entry.getKey(),(int)entry.getValue());
            }else if(entry.getValue() instanceof Boolean){
                intent.putExtra(entry.getKey(),(Boolean) entry.getValue());
            }else if(entry.getValue() instanceof Float){
                intent.putExtra(entry.getKey(),(Float) entry.getValue());
            }else if(entry.getValue() instanceof Double){
                intent.putExtra(entry.getKey(),(Double) entry.getValue());
            }else if(entry.getValue() instanceof Parcelable){
                intent.putExtra(entry.getKey(),(Parcelable) entry.getValue());
            }else if(entry.getValue() instanceof Serializable){
                intent.putExtra(entry.getKey(),(Serializable) entry.getValue());
            }
        }
        context.startActivity(intent);
    }

    public abstract T getBinding();

}
